#!/usr/bin/env python
# -*- coding: utf-8 -*-
from flask_script import Manager, Server, Shell
from flask_migrate import Migrate, MigrateCommand
# from flask.ext.script import Manager
# from flask.ext.migrate import Migrate, MigrateCommand
from AdminApiRunner.app import app, db
from AdminApiRunner.app.models import User, Admin
from AdminApiRunner.core.Utils import PasswdUtil

manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)

@manager.command
def init_db():
    """
    初始化数据库
    """
    db.create_all()

    admin_role = Admin(id=1, name="Admin",password=PasswdUtil.md5("123456"))
    user_role = User(id=2, username="dqiang", password=PasswdUtil.md5("123456"))

    db.session.add_all([admin_role, user_role])
    try:
        db.session.commit()
    except Exception as err:
        db.session.rollback()
        print('Exception: ', err)

if __name__ == "__main__":
    manager.run()

# python manage.py db init   初始化数据库,会创建一个migations文件夹,并且会在数据库中生成一个alembic_version表
# python manage.py db migrate  创建迁移历史
# python manage.py db upgrade  更新数据库